package com.ljh.redis.Controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Set;

/**
 * @author: Themberfue
 * @date: 2025/4/5 15:49
 * @description:
 */
@RestController
public class ZsetController {
    @Autowired
    StringRedisTemplate redisTemplate;

    @GetMapping("/zset")
    public Set<ZSetOperations.TypedTuple<String>> zset() {
        redisTemplate.execute((RedisConnection connection) -> {
            connection.flushAll();
            return null;
        });

        redisTemplate.opsForZSet().add("k1", "zhangsan", 10.0);
        redisTemplate.opsForZSet().add("k1", "lishi", 20.0);
        redisTemplate.opsForZSet().add("k1", "wangwu", 30.0);
        redisTemplate.opsForZSet().add("k1", "zhaoliu", 40.0);

        Long remove = redisTemplate.opsForZSet().remove("k1", "wangwu");
        System.out.println("remove:" + remove);

        Long rank = redisTemplate.opsForZSet().rank("k1", "lishi");
        System.out.println("rank:" + rank);

        Long size = redisTemplate.opsForZSet().size("k1");
        System.out.println("size:" + size);

        return redisTemplate.opsForZSet().rangeWithScores("k1", 0, -1);
    }
}
